package flca.voicenav;

import java.io.File;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import flca.voicenav.util.FileUtils;

public class RouteListActivity extends ListActivity implements VoiceNavConstants, OnItemClickListener
{
	ListView listView;

	private String mFiles[];
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		List<String> fileList = FileUtils.findFiles(FILES_ROOT, false);
		if (fileList != null && fileList.size()  > 0) {
			mFiles = parseFiles(fileList);

			setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, mFiles));

			ListView listView = getListView();
			listView.setTextFilterEnabled(true);
			listView.setOnItemClickListener(this);
		} else {
			Intent intent = getIntent();
			intent.putExtra(SELC_FNAME, (String) null);
			setResult(RESULT_CANCELED, intent);
			finish();
		}
	}

	private String[] parseFiles(List<String> fileList)
	{
		mFiles = new String[fileList.size()];
		for (int i=0; i<fileList.size(); i++) {
			String fname = FileUtils.getFilenameOnly(new File(fileList.get(i)));
			mFiles[i] = fname.substring(0, fname.length()-4);
		}
		return mFiles;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			int position, long id)
	{
		Intent intent = getIntent();
		String url = "file:///" + FILES_ROOT + "/" + mFiles[position] + ".gpx";
		intent.putExtra(SELC_FNAME, url);
		setResult(RESULT_OK, intent);
		finish();
	}


}
